/*
 * The Yard Utilties - http://www.theyard.net/
 * 
 * Copyright (c) 2008 by Vlideshow, Inc..  All Rights Resrved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation; either version 2.1 of the License, or (at your option) any later 
 * version. 
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along 
 * with this library; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package net.theyard.components.netconnectionsm
{

  import flash.utils.Proxy;
  import flash.utils.flash_proxy;
  import flash.utils.getDefinitionByName;
  import flash.utils.getQualifiedClassName;
  import net.theyard.components.Utils;

  /**
   * Use this class to implement any call backs that you expect the server
   * to execute on our net connection.
   * <p>
   * To use, derive from this class and then implement a method for every
   * function the server can call back to,
   * </p>
   * <p>
   * What's useful about this (versus just setting the NetConnection#client
   * property directly) is that if the server calls a method that you
   * haven't defined on this object (for example because of a typo)
   * the methodMissing() method will be called and will log the problem.
   * </p>
   * <p>
   * You can then derive from methodMissing() and do your own handling.
   * </p>
   */
  public dynamic class NetConnectionCallDelegate extends Proxy
  {

    /**
     * Create the delegate.
     */
    public function NetConnectionCallDelegate()
    {
    }

    /**
     * This is used by the Flash proxy to request any property.  We
     * return a function that will call our special lookup function
     * anytime the property is actually referenced.
     *
     * <p><b>DO NOT OVERRIDE IN A DERIVING CLASS</b></p>
     *
     * @param aName The name of the property being searched for.
     * @return Can return anything a function can return.
     */
    flash_proxy override function getProperty(aName:*):*
    {
      var context:*=this;
      var methodName:*=aName;
      return function(...aArgs):* {
        try
        {
          aArgs.unshift(methodName);
          return context.callMethod.apply(context, aArgs);
        }
        catch (e:Error)
        {
          return undefined;
        }
      }
    }

    /**
     * This is called by the closure method on the Flash proxy getProperty
     * function.  It looks within this class (or children if extended)
     * for the method, and if it finds it, executes it.
     *
     * If it doesn't find it, it executes methodMissing which can be
     * overriden by any child.
     */
    private function callMethod(aMethod:*, ...aArgs):*
    {
      try
      {
        // First try executing the method on this object, and if it's
        // not here, then delegate to methodMissing...

        var clazz:Class
          = getDefinitionByName(getQualifiedClassName(this)) as Class;
        return clazz.prototype[aMethod].apply(this, aArgs);
      }
      catch (e:Error)
      {
        aArgs.unshift(aMethod);
        return methodMissing.apply(this, aArgs);
      }
    }

    /**
     * Override this method if you want to be notified whenever a
     * method is called from the server but not implemented on this client.
     *
     * The default implemention calls Utils.ytrace and then throws an Error
     *
     * @param aMethod The method name the server looked for
     * @param aArgs The arguments the server passed
     *
     * @return can return anything
     * @throws Error Throws Error if not overriden.
     *
     */
    protected function methodMissing(aMethod:*, ...aArgs):*
    {
      Utils.ytrace("Missing method: " + (aMethod != null ? aMethod.toString() : ""));
      throw new Error("Missing method: " + (aMethod != null ? aMethod.toString() : ""));
    }
  }
}
